import { Suspense } from 'react'
import { Redirect, Route } from 'react-router'
import { getToken } from '@/utils/tools'
import Loading from '@/components/Loading'

const PrivateRouter = ({ component: Component, ...rest }) => {
  return (
    <Route {...rest} render={
      routeProps => getToken() ?
        <Suspense fallback={<Loading/>}><Component {...routeProps} /></Suspense>
        : <Redirect to={{pathname: '/login', state: { from: routeProps.location }}} />
    } />
  )
}

export default PrivateRouter